package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.custom.AjaxResult;
import com.example.demo.entity.SysDept;
import com.example.demo.form.DeptForm;
import com.example.demo.service.SysDeptService;


@RestController
@RequestMapping(value = "/dept")
public class DeptController {
	@Autowired
	private SysDeptService deptService;
	
	@RequestMapping(value = "/combotree")
	@ResponseBody
	public Object combotree(Integer id) {
		Sort sort = Sort.by("idx1");
		Specification<SysDept> spec = buildSpec1();
		List<SysDept> list = deptService.findAll(spec, sort);
		List<HashMap<String, Object>> result=buildTree(list,id);
		return new AjaxResult(result);
	}
	
	private List<HashMap<String, Object>> buildTree(List<SysDept> list, Integer id) {
		List<HashMap<String, Object>> result=new ArrayList<>();
		for (SysDept dept : list) {
			if(dept.getId()!=id) {
				HashMap<String, Object> node=new HashMap<>();
				node.put("id", dept.getId());
				node.put("name", dept.getName());
				node.put("pid", dept.getParentId());
				node.put("idx",dept.getIdx1());
				if(dept.getChildren().size()>0) {
					node.put("childrens", buildTree(dept.getChildren(), id));
				}
				if(dept.getParentId()!=null) {
					node.put("pidname", deptService.findById(dept.getParentId()).getName());
				}
				result.add(node);
			}
		}
		return result;
	}

	@SuppressWarnings("serial")
	private Specification<SysDept> buildSpec1(){
		Specification<SysDept> spec=new Specification<SysDept>() {
			@Override
			public Predicate toPredicate(Root<SysDept> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				return criteriaBuilder.isNull(root.get("parent"));
			}
		};
		return spec;
	}

	@RequestMapping(value = "/save")
	public Object save(DeptForm form) {
		try {
			SysDept model=new SysDept();
			Integer id = form.getId();
			if(id!=null) {
				model=deptService.findById(id);
			}
			Integer parentId = form.getParentId();
			if(parentId!=null) {
				model.setParent(deptService.findById(parentId));
			}else {
				model.setParent(null);
			}

			BeanUtils.copyProperties(form, model,"id","parent");
			deptService.save(model);
			return new AjaxResult("数据保存成功！");
			
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}
	
	@RequestMapping(value = "/delete")
	public Object delete(Integer id) {
		deptService.deleteById(id);
		return new AjaxResult("删除成功");
	}
}	
